<template>
  <el-container>
    <el-header class="searchBox">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <!-- label="模糊查询" -->
          <el-input v-model="params.keywords" placeholder="角色名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-auth="1020201" type="primary" icon="el-icon-search" @click="handleSearch()">查询</el-button>
          <el-button v-auth="1020202" type="primary" icon="el-icon-plus" @click="handleAdd()">新增</el-button>
          <!-- <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="handleBatchRemove()">批量删除</el-button> -->
        </el-form-item>
      </el-form>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="listData" v-bind:params="params" row-key="id" @selection-change="selectionChange" stripe border remoteSort>
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="角色名称" prop="name" width="150" sortable></el-table-column>
        <el-table-column label="别名" prop="alias" width="200" sortable></el-table-column>
        <el-table-column label="排序" prop="sort" width="80" sortable></el-table-column>
        <el-table-column label="状态" prop="status" width="100">
          <template #default="scope">
            <el-switch v-model="scope.row.status" @change="changeSwitch(scope.row)" :loading="scope.row.$switchStatus" :active-value="true" :inactive-value="false"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="180" sortable></el-table-column>
        <el-table-column label="备注" prop="remarks" width="380"></el-table-column>
        <el-table-column label="操作" align="left">
          <template #default="scope">
            <el-button-group>
              <el-button v-auth="1020203" text type="primary" size="small" icon="el-icon-menu" @click="handlePermission(scope.row, scope.$index)">设置权限</el-button>
              <el-button v-auth="1020204" text type="primary" size="small" icon="el-icon-avatar" @click="handleUser(scope.row, scope.$index)">设置人员</el-button>
              <el-button v-auth="1020205" text type="primary" size="small" icon="el-icon-search" @click="handleShow(scope.row, scope.$index)">查看</el-button>
              <el-button v-auth="1020206" text type="primary" size="small" icon="el-icon-edit" class="btnBlue1" @click="handleEdit(scope.row, scope.$index)">编辑</el-button>
              <el-popconfirm  title="确定删除吗？" @confirm="handleRemove(scope.row, scope.$index)">
                <template #reference>
                  <el-button v-auth="1020207" type="text" size="small" icon="el-icon-remove" class="btnRed">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <!-- 编辑等操作弹窗 -->
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="callBack" @closed="dialog.save = false">
  </save-dialog>
  <!-- 菜单授权弹窗 -->
  <permission-dialog v-if="dialog.permission" ref="permissionDialog" @closed="dialog.permission = false">
  </permission-dialog>
  <!-- 人员设置弹窗 -->
  <userset-dialog v-if="dialog.userset" ref="usersetDialog"  @closed="dialog.userset = false">
  </userset-dialog>
</template>

<script>
//引用对象
import saveDialog from "./components/save";
import permissionDialog from "./components/permission";
import usersetDialog from "./components/userset";
export default {
  name: "role",
  components: {
    saveDialog,
    permissionDialog,
    usersetDialog
  },
  data() {
    return {
      params: {
        keywords: "",
      },
      listData: this.$API.role.getPageList, //表格返回信息
      selection: [],
      dialog: {
        save: false,
        permission: false,
        userset: false,
      },
    };
  },
  /**html加载完成之前，执行。执行顺序：父组件-子组件 */
  created() { },
  /**[页面初始化方法]html加载完成后执行。执行顺序：子组件-父组件*/
  mounted() { },
  methods: {
    //获取数据
    handleSearch() {
      this.$refs.table.loadData(this.params);
    },
    //查看
    handleShow(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("show").setData(row);
      });
    },
    //添加
    handleAdd() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open();
      });
    },
    //编辑
    handleEdit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    //表格内开关
    async changeSwitch(row) {
      row.$switchStatus = true;
      var par = {
        id: row.id
      };
      var res = await this.$API.role.forbidOrEnable.put(par);
      if (res.code != 200) {
        this.$Message.warning(res.msg);
      }
      delete row.$switchStatus;
    },
    //权限设置
    handlePermission(row, index) {
      this.dialog.permission = true;
      this.$nextTick(() => {
        this.$refs.permissionDialog.open(row);
      });
    },
    //人员设置
    async handleUser(row, index) {
      this.dialog.userset = true;
      this.$nextTick(() => {
        this.$refs.usersetDialog.open(row);
      });
    },
    //单条删除
    async handleRemove(row, index) {
      var reqData = { id: row.id }
      var res = await this.$API.role.deleteById.delete(reqData);
      if (res.code == 200) {
        //这里选择刷新整个表格 OR 插入/编辑现有表格数据
        this.$refs.table.tableData.splice(index, 1);
        this.$Message.success("删除成功");
      } else {
        this.$Message.warning(res.msg);
      }
    },
    //表格选择后回调事件
    selectionChange(selection) {
      this.selection = selection;
    },
    //批量删除
    async handleBatchRemove() {
      if (this.selection.length == 0) {
        this.$Message.warning("请选择需删除项");
      }
      else {
        var par = {
          ids: []
        };
        this.selection.forEach(element => {
          par.ids.push(element.id);
        });
        var res = await this.$API.role.deleteByIds.delete(par);
        if (res.code === 200) {
          var loading = this.$loading();
          par.ids.forEach(itemid => {
            this.$refs.table.tableData.forEach((itemI, indexI) => {
              if (itemid === itemI.id) {
                this.$refs.table.tableData.splice(indexI, 1)
              }
            })
          })
          loading.close();
          this.$Message.success("删除成功");
        } else {
          this.$Message.warning(res.msg);
        }
      }
    },
    //本地更新数据[回调]
    callBack(data, mode) {
      if (mode == 'add') {
        this.$refs.table.reloadData();
      } else if (mode == 'edit') {
        this.$refs.table.tableData.filter(item => item.id === data.id).forEach(item => {
          Object.assign(item, data);
        });
      }
    },
  },
};
</script>
<style>
</style>
